using System;
using System.Collections.Generic;
using DG.Tweening;
using DoTweenExtends;
using Sirenix.OdinInspector;
using UnityEngine.Events;

namespace DoTweenExtends
{
    public class TweenSequence : TweenBase
    {
        public enum TweenAction
        {
            Append, // 顺序添加动画
            AppendCallback, // 顺序添加动画并回调
            Join, // 与上一个动画并行
            Interval // 插入间隔
        }

        [Serializable]
        public class SequenceInfo
        {
            public TweenAction actionType = TweenAction.Append;

            [ShowIf("ShouldTweenItem", false), FoldoutGroup("TweenItem"), HideLabel]
            public SequenceItem sequenceItem;

            [ShowIf("ShouldInterval", false)]
            public float interval;

            [ShowIf("ShouldCallback", false)]
            public UnityEvent callback;

            private bool ShouldTweenItem()
            {
                return actionType == TweenAction.Append || actionType == TweenAction.Join;
            }

            private bool ShouldInterval()
            {
                return actionType == TweenAction.Interval;
            }

            private bool ShouldCallback()
            {
                return actionType == TweenAction.AppendCallback;
            }
        }

        [Title("Sequence")]
        public List<SequenceInfo> tweenItems = new List<SequenceInfo>();

        protected override Tween CreateTween()
        {
            Sequence sequence = DOTween.Sequence();

            foreach (SequenceInfo item in tweenItems)
            {
                switch (item.actionType)
                {
                    case TweenAction.Append:
                        Tween appendTween = item.sequenceItem.CreateTween();
                        if (appendTween != null)
                        {
                            sequence.Append(appendTween);
                        }

                        break;

                    case TweenAction.Join:
                        Tween joinTween = item.sequenceItem.CreateTween();
                        if (joinTween != null)
                        {
                            sequence.Join(joinTween);
                        }

                        break;

                    case TweenAction.Interval:
                        sequence.AppendInterval(item.interval);
                        break;
                    case TweenAction.AppendCallback:
                        sequence.AppendCallback(item.callback.Invoke);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }

            return sequence;
        }
    }
}